package com.drought.rained.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.bean.OrderDetailDataLJ;
import com.drought.rained.bean.OrderItemDataLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.utils.StringUtilsLJ;

import java.util.List;

public class BillAdapterLJ extends BaseRecyclerAdapterLJ<OrderItemDataLJ, BillViewHolderLJ> {

    private OnBliiDetailListener listener;
    private boolean rel = false; // 展期可配

    public BillAdapterLJ(Context context, OnBliiDetailListener listener) {
        super(context);
        this.listener = listener;
    }

    public void setRel(boolean rel) {
        this.rel = rel;
    }

    public List<OrderItemDataLJ> getData() {
        return mDatas;
    }

    public void setOrderDetail(int position, OrderDetailDataLJ orderDetail) {
        mDatas.get(position).setOrderDetail(orderDetail);
        notifyItemChanged(position);
    }

    @NonNull
    @Override
    public BillViewHolderLJ onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BillViewHolderLJ(inflater.inflate(R.layout.item_of_bill_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final BillViewHolderLJ billViewHolder, int position) {
        final OrderItemDataLJ orderItem = mDatas.get(position);
        billViewHolder.tvOrderId.setText(orderItem.getOrderId());
        setBillType(billViewHolder, orderItem);
        billViewHolder.tvDeadline.setText(orderItem.getProductTerm()); // 产品周期

        billViewHolder.btnRollover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouterMapLJ.ROLLOVER_PAGE)
                        .withString(ConstantLJ.ORDER_ID, orderItem.getOrderId())
                        .withInt(ConstantLJ.ORDER_LIST_TYPE, orderItem.getOrderStatus())
                        .withString(ConstantLJ.ORDER_PRODUCT_TERM, orderItem.getProductTerm())
                        .navigation();
            }
        });
        billViewHolder.btnRepayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isRollover = false;
                ARouter.getInstance()
                        .build(RouterMapLJ.REPAY_METHOD_PAGE)
                        .withString(ConstantLJ.ORDER_ID, orderItem.getOrderId())
                        .withString(ConstantLJ.ORDER_MONEY_NUM, String.valueOf(orderItem.getRepayMoney()))
                        .withBoolean(ConstantLJ.ORDER_MONEY_IS_ROLLOVER, isRollover)
                        .navigation();
            }
        });
    }

    /**
     * 审批状态:审核中1 待放款2 已放款 3 已拒绝4 已结清5 已逾期6 已展期7
     */
    private void setBillType(BillViewHolderLJ holder, OrderItemDataLJ orderItem) {
        String typeText = "";
        String OrderCashNum = StringUtilsLJ.getDotMoneyStr(String.valueOf(orderItem.getApprovalMoney())); // 申请金额
        switch (orderItem.getOrderStatus()) {
            case ConstantLJ.ORDER_STATUS_AUDITING:
                typeText = getStringResource(R.string.bill_type_auditing);
                OrderCashNum = StringUtilsLJ.getDotMoneyStr(String.valueOf(orderItem.getApplyMoney()));
//                setVisibility(holder.imgArrows, View.GONE);
                setVisibility(holder.btnLayout, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
            case ConstantLJ.ORDER_STATUS_WAITLEND:
                typeText = getStringResource(R.string.bill_type_wait_lend);
//                setVisibility(holder.imgArrows, View.GONE);
                setVisibility(holder.btnLayout, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
            case ConstantLJ.ORDER_STATUS_LENDING:
                typeText = getStringResource(R.string.bill_type_lending);
//                setVisibility(holder.imgArrows, View.VISIBLE);
                setVisibility(holder.btnLayout, View.VISIBLE);
                setVisibility(holder.btnRollover, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
            case ConstantLJ.ORDER_STATUS_REJECT:
                typeText = getStringResource(R.string.bill_type_reject);
                OrderCashNum = StringUtilsLJ.getDotMoneyStr(String.valueOf(orderItem.getApplyMoney()));
//                setVisibility(holder.imgArrows, View.GONE);
                setVisibility(holder.btnLayout, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
            case ConstantLJ.ORDER_STATUS_CLOSEOUT:
                typeText = getStringResource(R.string.bill_type_closeout);
//                setVisibility(holder.imgArrows, View.VISIBLE);
                setVisibility(holder.btnLayout, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
            case ConstantLJ.ORDER_STATUS_OVERDUE:
                typeText = getStringResource(R.string.bill_type_overdue);
//                setVisibility(holder.imgArrows, View.VISIBLE);
                setVisibility(holder.btnLayout, View.VISIBLE);
                if (rel) {
                    setVisibility(holder.btnRollover, View.VISIBLE);
                    setVisibility(holder.view,View.VISIBLE);
                } else {
                    setVisibility(holder.btnRollover, View.GONE);
                    setVisibility(holder.view,View.GONE);
                }
                break;
            case ConstantLJ.ORDER_STATUS_ROLLOVER:
                typeText = getStringResource(R.string.bill_type_go_to_repay);
//                setVisibility(holder.imgArrows, View.VISIBLE);
                setVisibility(holder.btnLayout, View.VISIBLE);
                setVisibility(holder.btnRollover, View.GONE);
                setVisibility(holder.view,View.GONE);
                break;
        }
        holder.tvBillType.setText(typeText);
        holder.tvOrderCashNum.setText(OrderCashNum);
    }

    private void setViewStub(ImageView imgArrows, View view) {
        float[] rotation = view.getVisibility() == View.GONE ? new float[]{0f, 180f} : new float[]{180f, 0f};
        ObjectAnimator animator = ObjectAnimator.ofFloat(imgArrows, ConstantLJ.PROPERTY_ROTATION, rotation);
        animator.setDuration(500);
        animator.start();

        setVisibility(view);

    }

    private String getStringResource(int strResourceId) {
        return mContext.getResources().getString(strResourceId);
    }

    private void setVisibility(View view, int... visibility) {
        if (visibility.length > 0) {
            if (view.getVisibility() == visibility[0]) return;
            view.setVisibility(visibility[0]);
        } else {
            view.setVisibility(view.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
        }

    }

    public interface OnBliiDetailListener {
        void onBliiDetailClick(View view, int position);
    }
}
